package aiku.numericalcontroloa.common.utils;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间工具，格式转换
 * @Author 黄广庭
 * @Date 2024/3/2 15:06
 */
public class DateUtils {

    /**
     * 年月日字符串日期格式转换
     * @param YMD 字符串格式“yyyy-MM-dd”
     * @return 返回"E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"格式
     */
    public static String YMDFormatDateString(String YMD){
        LocalDate localDate = LocalDate.parse(YMD);
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.of("Asia/Shanghai"));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", java.util.Locale.US);
        return zonedDateTime.format(formatter);
    }

    /**
     * 转换为年月日格式
     * @param date 格式为“E MMM dd HH:mm:ss z yyyy”
     * @return 返回“yyyy-MM-dd”格式
     */
    public static String dateStringFormatYMD(String date){
        // 定义日期时间格式
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", java.util.Locale.US);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 解析输入日期字符串并格式化为所需格式
        LocalDateTime dateTime = LocalDateTime.parse(date, inputFormatter);
        String formattedDate = dateTime.format(outputFormatter);
        //System.out.println("Formatted Date==>" + formattedDate);
        return formattedDate;
    }

    /**
     * 时分秒字符串日期格式转换
     * @param timeString 格式“HH:mm:ss”
     * @return 返回"E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"格式
     */
    public static String HMSFormat(String timeString){
        SimpleDateFormat inputFormat = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat outputFormat = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)",java.util.Locale.US);
        try {
            Date date = inputFormat.parse(timeString);
            String formattedDate = outputFormat.format(date);
            //System.out.println(formattedDate); // 输出类似 "Fri Mar 01 2024 08:30:00 GMT+0800"
            return formattedDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Date转String
     * @param date 格式“EEE MMM dd HH:mm:ss zzz yyyy”
     * @return 返回“yyyy-MM-dd HH:mm:ss”格式
     */
    public static String YMDHMSFormat(Date date){
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = outputFormat.format(date);
//        System.out.println("转换后的日期: " + formattedDate);
        return formattedDate;
    }

    /**
     * String转Date
     * @param date 格式“yyyy-MM-dd HH:mm:ss”
     * @return 返回Date
     */
    public static Date YMDHMSFormatDate(String date){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
        Date formattedDate = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("转换后的日期: " + formattedDate);
        return formattedDate;
    }

    /**
     * 比较java.sql.Time大小；
     * -1，time1小于time2；
     * 0，time1等于time2；
     * 1，time1大于time2；
     * @param time1
     * @param time2
     * @return -1,0,1
     */
    public static int compareToTime(Time time1,Time time2){
        // 获取time1的时分秒
        int time1Hours = time1.getHours();
        int time1Minutes = time1.getMinutes();
        int time1Seconds = time1.getSeconds();
        // 计算time1的总秒数：(时*60+分)*60+秒
        long time1TotalSeconds = (time1Hours*60+time1Minutes)*60+time1Seconds;
        // 获取time2的时分秒
        int time2Hours = time2.getHours();
        int time2Minutes = time2.getMinutes();
        int time2Seconds = time2.getSeconds();
        // 计算time2的总秒数：(时*60+分)*60+秒
        long time2TotalSeconds = (time2Hours*60+time2Minutes)*60+time2Seconds;
        // 比较大小
        return Long.compare(time1TotalSeconds, time2TotalSeconds);
    }

    /**
     * java.sql.Time2减去java.sql.Time1
     * @param time2
     * @param time1
     * @return
     */
    public static long subtractionToTime(Time time2, Time time1){
        // 获取time1的时分秒
        int time1Hours = time1.getHours();
        int time1Minutes = time1.getMinutes();
        int time1Seconds = time1.getSeconds();
        // 计算time1的总秒数：(时*60+分)*60+秒
        long time1TotalSeconds = (time1Hours*60+time1Minutes)*60+time1Seconds;
        // 获取time2的时分秒
        int time2Hours = time2.getHours();
        int time2Minutes = time2.getMinutes();
        int time2Seconds = time2.getSeconds();
        // 计算time2的总秒数：(时*60+分)*60+秒
        long time2TotalSeconds = (time2Hours*60+time2Minutes)*60+time2Seconds;
        // time2-time1
        return time2TotalSeconds-time1TotalSeconds;
    }
}
